﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLUNZ.Admin;
using Telerik.Web.UI;

namespace NLUNZ.UserControls
{
    public partial class SlideImagesProperty : System.Web.UI.UserControl
    {
        private string _targetFolder;
        private string _targetFile = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            _targetFolder = Server.MapPath("~/Images/slideshow/");
            showMessage.Text = string.Empty;
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string url = Request.Url.ToString();
            Page.Response.Redirect(url);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string imgUrl = string.Empty;
            if (RadUpload1.UploadedFiles.Count > 0)
            {
                foreach (UploadedFile file in RadUpload1.UploadedFiles)
                {
                    imgUrl = _targetFile == string.Empty ? file.GetName() : _targetFile;

                    string filePath = _targetFolder + imgUrl;

                    Size size = ImageHelper.GetDimensions(filePath);
                    StringBuilder message = new StringBuilder();
                    if (size.Height < Repository.SLIDESHOW_HEIGHT || size.Width < Repository.SLIDESHOW_WIDTH)
                    {
                        DeleteUploadedImage(imgUrl);

                        message.AppendFormat("<div class=\"error\">The image {0} width: {1}, height: {2}", imgUrl, size.Width,
                                             size.Height);
                        message.AppendFormat(
                            "<br /><b>The Image size is smaller than the minimum of required image size. please make sure your image minimum size is width: {0}, height: {1}!</b></div>", Repository.SLIDESHOW_HEIGHT, Repository.SLIDESHOW_WIDTH);


                        showMessage.Text = message.ToString();
                        resetUploadForm();
                    }
                    else
                    {
                        string title = txtImageTitle.Text;
                        string name = txtImageName.Text;
                        string url = txtImageHyperLink.Text;
                        string desc = txtImageDescription.Text;

                        Repository.InsertSideshow(title, name, url, desc,imgUrl);
                        resetUploadForm();
                        showMessage.Text = "<div class=\"success\">The file uploaded successfully.</div>";
                    }

                }
            }
        }

        private void resetUploadForm()
        {
            txtImageDescription.Text = string.Empty;
            txtImageHyperLink.Text = string.Empty;
            txtImageName.Text = string.Empty;
            txtImageTitle.Text = string.Empty;
        }

        private void DeleteUploadedImage(string filename)
        {
            string filePath = _targetFolder + filename;
            FileInfo info = new FileInfo(filePath);
            info.Delete();
        }

        protected void RadUpload1_FileExists(object sender, Telerik.Web.UI.Upload.UploadedFileEventArgs e)
        {

            Guid filename = Guid.NewGuid();
            _targetFile = filename + e.UploadedFile.GetExtension();
            string destination = _targetFolder + _targetFile;
            e.UploadedFile.SaveAs(destination);
        }
    }
}